package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessReward;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.TrainingListVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.xml.UserXML;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TechnologyTrainingVideoDetailActivity extends BaseActivity {


    @Bind(R.id.technology_training_video_detail_web_view)
    WebView webView;
    @Bind(R.id.tv_reward_num)
    TextView tvRewardNum;
    @Bind(R.id.tv_coin_num)
    TextView tvCoinNum;

    private String url = "";
    private String id = "";
    private TrainingListVO vo;

    @Override
    public void setLayout() {
        setTitle("视频直播详情");
        setContentLayout(R.layout.activity_technology_training_video_detail);
    }

    @Override
    public void initMember() {

        vo = (TrainingListVO) getIntent().getSerializableExtra("TrainingListVO");

        if (vo == null)
            return;

        url = vo.getVideo() + "";
        id = vo.getId() + "";

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setUseWideViewPort(true); // 关键点
        webSettings.setAllowFileAccess(true); // 允许访问文件
        webSettings.setSupportZoom(true); // 支持缩放
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); // 不加载缓存内容

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webView.getSettings().setUseWideViewPort(true);

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {

            }

            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                //super.onReceivedSslError(view, handler, error);
                handler.proceed();
            }

            //覆盖shouldOverrideUrlLoading 方法
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url == null) return false;

                //处理http和https开头的url
                if (url.startsWith("http://") || url.startsWith("https://"))
                    webView.loadUrl(url);
                return true;
            }
        });

        webView.loadUrl(url);

        BusinessReward.queryRewardInfo(this, id, mHandler);
    }

    @Override
    public void addListener() {

    }

    // 覆盖onKeydown 添加处理WebView 界面内返回事件处理
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_QUERY_REWARD_INFO_SUCCESS:
                        String count = FastJsonUtil.getString(msg.obj.toString(), "count");
                        String money = FastJsonUtil.getString(msg.obj.toString(), "money");
                        tvRewardNum.setText(count + "");
                        tvCoinNum.setText(money + "");
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.iv_reward, R.id.layout_history_reward, R.id.layout_consult, R.id.layout_comment})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_reward:
                if ("1".equals(UserXML.getLoginStatus(TechnologyTrainingVideoDetailActivity.this))) {
                    Map<String, String> map = new HashMap<>();
                    map.put("redType", "1");
                    map.put("agriConsultId", id);
                    GotoUtil.gotoForResultActivity(TechnologyTrainingVideoDetailActivity.this, RewardActivity.class, 135, "map", map);
                } else {
                    GotoUtil.gotoActivity(TechnologyTrainingVideoDetailActivity.this, LoginGuideActivity.class);
                }
                break;
            case R.id.layout_history_reward:
                GotoUtil.gotoActivity(TechnologyTrainingVideoDetailActivity.this, RewardHistoryActivity.class, "agriConsultId", id);
                break;
            case R.id.layout_consult:
                if (!Utils.isLogin(TechnologyTrainingVideoDetailActivity.this)) {
                    startActivityForResult(new Intent(TechnologyTrainingVideoDetailActivity.this, LoginActivity.class), 1);
                    return;
                }
               /* if (vo.getExpertInfo() == null)
                    return;
                GotoUtil.gotoActivity(TechnologyTrainingVideoDetailActivity.this, ConsultIssueV2Activity.class, "single", vo.getExpertInfo());*/
                break;
            case R.id.layout_comment:
                GotoUtil.gotoActivity(TechnologyTrainingVideoDetailActivity.this, TechnologyTrainingVideoDetailCommentActivity.class, "TrainingListVO", vo);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 135) {
                BusinessReward.queryRewardInfo(this, id, mHandler);
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        webView.onPause();
        webView.pauseTimers();
    }

    @Override
    public void onResume() {
        super.onResume();
        webView.resumeTimers();
        webView.onResume();
    }


    @Override
    protected void onDestroy() {
        webView.destroy();
        webView = null;
        super.onDestroy();
    }
}